package cn.dckeji.dcssm.controller;

import cn.dckeji.dcssm.common.enums.ProjectEnum;
import cn.dckeji.dcssm.common.model.ResultModel;
import cn.dckeji.dcssm.entity.ErrorLog;
import cn.dckeji.dcssm.service.ErrorLogService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @author kenny
 * @Title: ErrorLogController
 * @ProjectName dcssm
 * @Description: 错误日志控制器
 * @date 2019/3/4 14:28
 */
@RestController
@RequestMapping("/errorLog")
public class ErrorLogController {

    @Autowired
    private ErrorLogService errorLogService;

    /**
     * 分页查询错误日志列表
     * @param pageNumber
     * @param pageSize
     * @param dateSelect
     * @return
     */
    @GetMapping("/page")
    public JSONObject page(Integer pageNumber, Integer pageSize, @RequestParam(required = false) String dateSelect) {
        QueryWrapper<ErrorLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_create");
        if (StringUtils.isNotBlank(dateSelect)) {
            String startDate = dateSelect.substring(0, 11);
            String endDate = dateSelect.substring(dateSelect.length() - 10);
            queryWrapper.between("gmt_create", startDate+" 00:00:00", endDate+ " 23:59:59");
        }
        IPage<ErrorLog> page = errorLogService.page(new Page<>(pageNumber, pageSize), queryWrapper);
        return ResultModel.page(page.getTotal(),page.getRecords());
    }

    /**
     * 标记已查看
     * @param errorLogId
     * @return
     */
    @PostMapping("/updateIsView")
    public ResultModel<Object> updateIsView(String errorLogId) {
        ErrorLog errorLog = errorLogService.getById(errorLogId);
        errorLog.setView(ProjectEnum.ERRORLOG_ISVIEW.code());
        errorLog.setGmtModified(LocalDateTime.now());
        errorLogService.updateById(errorLog);
        return ResultModel.success();
    }
}
